package org.dromara.business.modules.services.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.business.modules.services.domain.bo.BuServicesBo;
import org.dromara.business.modules.services.domain.vo.BuServicesVo;
import org.dromara.business.modules.services.service.IBuServicesService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.AuditBo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务信息
 *
 * @author ckk
 * @date 2025-10-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/services")
public class BuServicesController extends BaseController {

    private final IBuServicesService buServicesService;

    /**
     * 前台列表
     */
    @SaIgnore
    @GetMapping("/frontList")
    public TableDataInfo<BuServicesVo> frontList(BuServicesBo bo, PageQuery pageQuery) {
        return buServicesService.frontList(bo, pageQuery);
    }

    /**
     * 查询服务信息列表
     */
    @SaCheckPermission("system:services:list")
    @GetMapping("/list")
    public TableDataInfo<BuServicesVo> list(BuServicesBo bo, PageQuery pageQuery) {
        return buServicesService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出服务信息列表
     */
    @SaCheckPermission("system:services:export")
    @Log(title = "服务信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BuServicesBo bo, HttpServletResponse response) {
        List<BuServicesVo> list = buServicesService.queryList(bo);
        ExcelUtil.exportExcel(list, "服务信息", BuServicesVo.class, response);
    }

    /**
     * 获取服务信息详细信息
     *
     * @param id 主键
     */
    @SaIgnore
    @GetMapping("/{id}")
    public R<BuServicesVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(buServicesService.getInfo(id));
    }

    /**
     * 新增服务信息
     */
    @SaCheckPermission("system:services:add")
    @Log(title = "服务信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BuServicesBo bo) {
        return toAjax(buServicesService.insertByBo(bo));
    }

    /**
     * 修改服务信息
     */
    @SaCheckPermission("system:services:edit")
    @Log(title = "服务信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BuServicesBo bo) {
        return toAjax(buServicesService.updateByBo(bo));
    }

    /**
     * 删除服务信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:services:remove")
    @Log(title = "服务信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(buServicesService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 批量审核
     */
    @SaCheckPermission("system:services:audit")
    @Log(title = "服务信息", businessType = BusinessType.AUDIT)
    @RepeatSubmit()
    @PostMapping("/audit")
    public R<Void> audit(@RequestBody AuditBo bo) {
        return toAjax(buServicesService.audit(bo));
    }
}
